﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ClassCommon
{
    /// <summary>
    /// cookie 帮助类
    /// </summary>
    public class HttpCookieHelper
    {
        /// <summary>
        /// 网站的一级&二级域名 baidu.com
        /// </summary>
        private static readonly string Domain_Name = System.Configuration.ConfigurationManager.AppSettings["domain"];

        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="key">cookie名称</param>
        /// <param name="value">cookie的值</param>
        /// <param name="tip">过期时间值</param>
        /// <param name="unit">过期时间单位 y/M/d/h/m/s</param>
        /// <param name="domain">cookie域</param>
        /// <param name="isHttpOnly">是否httpOnly</param>
        /// <param name="isSecure">是否支持https</param>
        /// <param name="shareable">是否允许参与缓存输出</param>
        public static void SetCookie(
            string key,
            string value,
            int tip = 1,
            string unit = "d",
            string domain = "",
            bool isHttpOnly = true,
            bool isSecure = false,
            bool shareable = false)
        {
            if (string.IsNullOrEmpty(key)) throw new ArgumentException("cookie的键不能为空!");
            // 对cookie 进行对称加密
            //value = EncryptHelper.Encrypt(value, string.Empty);
            HttpCookie cookie = new HttpCookie(key, value);
            if (!string.IsNullOrEmpty(domain))
                cookie.Domain = Domain_Name;//设置域名

            switch (unit)
            {
                case "y":
                    cookie.Expires = DateTime.Now.AddYears(tip); break;
                case "M":
                    cookie.Expires = DateTime.Now.AddMonths(tip); break;
                case "d":
                    cookie.Expires = DateTime.Now.AddDays(tip); break;
                case "h":
                    cookie.Expires = DateTime.Now.AddHours(tip); break;
                case "m":
                    cookie.Expires = DateTime.Now.AddMinutes(tip); break;
                case "s":
                    cookie.Expires = DateTime.Now.AddSeconds(tip); break;
                default: break;
            }


            cookie.HttpOnly = isHttpOnly;
            // cookie.Shareable = shareable;
            cookie.Secure = isSecure;

            // 把cookie添加到当前响应流
            HttpContext.Current.Response.Cookies.Add(cookie);
        }


        /// <summary>
        /// 清除cookie
        /// </summary>
        /// <param name="cookieName"></param>
        public static void clearCookie(string cookieName)
        {
            if (string.IsNullOrEmpty(cookieName)) throw new ArgumentException("cookieName 不能为空!");

            HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName];
            if (cookie == null) return;

            cookie.Value = "";

            cookie.Expires = DateTime.Now.AddYears(-10);

            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 获取用户的userID
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns>-1未登录</returns>
        public static int GetUserID()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[ConfigSetting.Cookie_UserID];
            if (cookie == null) return -1;

            return Convert.ToInt32(EncryptHelper.Decrypt(cookie.Value, string.Empty));
        }

        /// <summary>
        /// 获取解密后的验证码()
        /// </summary>
        /// <returns></returns>
        public static string getValidateCode()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[ConfigSetting.Cookie_ValidateCode];
            if (cookie == null) return string.Empty;
            string code = EncryptHelper.Decrypt(cookie.Value, string.Empty);

            return code;
        }

        /// <summary>
        /// 获得手机验证码
        /// </summary>
        /// <returns></returns>
        public static string getPhoneValidateCode()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[ConfigSetting.Cookie_PhoneValidateCode];
            if (cookie == null) return string.Empty;

            string value = cookie.Value;

            return EncryptHelper.Decrypt(value);
        }


        /// <summary>
        /// 用户登出
        /// </summary>
        public static void Logout()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[ConfigSetting.Cookie_UserID];
            if (cookie == null) return;

            cookie.Expires = DateTime.Now.AddYears(-1);

            HttpContext.Current.Response.Cookies.Add(cookie);
        }


    }
}
